package com.bRoom.util;

import org.apache.commons.net.ftp.FTPClient;
import sun.net.ftp.FtpClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class FtpUtils{

    private  static  final String ftpIp=PropertiesUtil.getProperties("ftp.server.ip");

    private static  final String ftpUser=PropertiesUtil.getProperties("ftp.user");

    private static  final  String ftpPass=PropertiesUtil.getProperties("ftp.pass");

    private  String ip;
    private int port;

    private String user;

    private  String pass;

    private FTPClient ftpClient;

    public  static  boolean upload(List<File> fileList) throws IOException {
          FtpUtils ftpUtils=new FtpUtils(ftpIp,21,ftpUser,ftpPass);
          boolean result=ftpUtils.upload("img",fileList);
          return result;
    }

    private boolean   upload(String remotePath,List<File> fileList) throws IOException {
           boolean uploaded=true;
            FileInputStream fs=null;
            if (connectionService(this.ip,this.port,this.user,this.pass)){
                try {
                    ftpClient.changeWorkingDirectory(remotePath);
                    ftpClient.setBufferSize(1024);
                    ftpClient.setControlEncoding("utf-8");
                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                    ftpClient.enterLocalPassiveMode();
                    for(File fileItem:fileList){
                          fs=new FileInputStream(fileItem);
                          ftpClient.storeFile(fileItem.getName(),fs);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    uploaded=false;
                }finally {
                    fs.close();
                    ftpClient.disconnect();
                }
            }
             return uploaded;
    }

    private boolean  connectionService(String ip,int port,String user,String pass){
        ftpClient =new FTPClient();
        boolean isSuccess=false;
        try {
            ftpClient.connect(ip);
            isSuccess=ftpClient.login(user,pass);
        } catch (IOException e) {
            e.printStackTrace();

        }
        return isSuccess;
    }

    public FtpUtils(String ip, int port, String user, String pass) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pass = pass;
    }

    public static String getFtpIp() {
        return ftpIp;
    }

    public static String getFtpUser() {
        return ftpUser;
    }

    public static String getFtpPass() {
        return ftpPass;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }
}
